package com.biz.primus.ms.thirdlogin.oauth.service.qq;

import com.biz.primus.model.thirdlogin.enums.OAuthTypes;
import com.biz.primus.ms.thirdlogin.dao.po.OAuthMember;
import com.biz.primus.ms.thirdlogin.oauth.service.OAuthServiceDeractor;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.oauth.OAuthService;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONPath;

/**
 * @author jun.liu
 * @date 2017/9/29
 */
public class QQOAuthService extends OAuthServiceDeractor
{

    private static final String PROTECTED_RESOURCE_URL = "https://graph.qq.com/oauth2.0/me";

    public QQOAuthService(OAuthService oAuthService) {
        super(oAuthService, OAuthTypes.QQ);
    }

    @Override
    public OAuthMember getOAuthMember(Token accessToken)
    {
        OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
        this.signRequest(accessToken, request);
        Response response = request.send();
        OAuthMember oAuthMember = new OAuthMember();
        oAuthMember.setOAuthType(getOAuthType());
        Object result = JSON.parse(response.getBody().substring(9, response.getBody().length() - 3));
        oAuthMember.setOAuthId(JSONPath.eval(result, "$.openid").toString());
        return oAuthMember;
    }

}
